<script type="text/javascript" src="tiny_mce/jquery.tinymce.js"></script>
<script type="text/javascript" src="tiny_mce/tiny_mce.js"></script>
<style>
	#edit_area	{ z-index:10; position: absolute; width: 800px; left: 150px; top: 100px; }
</style>
<script type="text/javascript">
//<![CDATA[
function clear_form_elements(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}
$(document).ready(function(){
	
	$("#btn_add").colorbox({width:"80%",height:"650px", inline:true, href:"#inline_example1",overlayClose:false});
	$(".btn_edit").colorbox({width:"80%",height:"650px", inline:true, href:"#inline_example1",overlayClose:false});
	$(".btn_mail").colorbox({width:"80%",height:"650px", inline:true, href:"#inline_example1",overlayClose:false});
	$("#btn_add").live("click",function(){
		$("#id").val('');
		clear_form_elements($("#dv_host"));
		$("#dv_mail").hide();
		$("#dv_send_complete").hide();
		$("#dv_host").show();
	})
	$("#btn_edit").live("click",function(){
		clear_form_elements($("#dv_host"));
		
		var id = $(this).closest("tr").find("#hd_id").val();
		var website = $(this).closest("tr").find("#hd_website").val();
		var host_package = $(this).closest("tr").find("#hd_host_package").val();
		var host_expired_date = $(this).closest("tr").find("#hd_host_expired_date").val();
		var domain_expired_date = $(this).closest("tr").find("#hd_domain_expired_date").val();
		var ma_status = $(this).closest("tr").find("#hd_ma_start").val();
		var ma_start_date = $(this).closest("tr").find("#hd_ma_start_date").val();
		var ma_end_date = $(this).closest("tr").find("#hd_ma_end_date").val();
		var customer_name = $(this).closest("tr").find("#hd_customer_name").val();
		var customer_email = $(this).closest("tr").find("#hd_customer_email").val();
		var description = $(this).closest("tr").find("#hd_description").val(); 
		
		$("#id").val(id);
		$("#website").val(website);
		$("#host_package").val(host_package);
		$("#host_expired_date").val(host_expired_date);
		$("#domain_expired_date").val(domain_expired_date);
		$("#ma_status").val(ma_status);
		$("#ma_start_date").val(ma_start_date);
		$("#ma_end_date").val(ma_end_date);
		$("#customer_name").val(customer_name);
		$("#customer_email").val(customer_email);
		$("#description").val(description);
		$("#dv_send_complete").hide();
		$("#dv_mail").hide();
		$("#dv_host").show();
	})
	
	$("#btn_mail").live("click",function(){
		$("#subject").val('');
		$("#mail_id").val('');
		$("#dv_send_complete").hide();
		$("#dv_mail").show();

		var id = $(this).closest("tr").find("#hd_id").val();
		var website = $(this).closest("tr").find("#hd_website").val();
		var host_package = $(this).closest("tr").find("#hd_host_package").val();
		var host_expired_date = $(this).closest("tr").find("#hd_host_expired_date").val();
		var domain_expired_date = $(this).closest("tr").find("#hd_domain_expired_date").val();
		var ma_status = $(this).closest("tr").find("#hd_ma_start").val();
		var ma_start_date = $(this).closest("tr").find("#hd_ma_start_date").val();
		var ma_end_date = $(this).closest("tr").find("#hd_ma_end_date").val();
		var customer_name = $(this).closest("tr").find("#hd_customer_name").val();
		var customer_email = $(this).closest("tr").find("#hd_customer_email").val();
		var description = $(this).closest("tr").find("#hd_description").val(); 		

		$("#td_customer_name").html(customer_name);
		$("#td_website").html(website);
		$("#td_customer_email").html(customer_email);
		$("#hd_email").html(customer_email);
		$("#mail_id").val(id);
		
		$("#dv_mail").show();
		$("#dv_host").hide();		
		buildRTE(id);	
	})
	
	$("#btn_send").click(function(){		
		var id = $("#mail_id").val();
		var subject = $("#subject").val();
		var message = tinyMCE.get('editor').getContent();
		$.post("hosting/send_email",{
			'id':id,
			'subject':subject,
			'message':message
		},function(data){
			$("#dv_mail").hide();
			$("#dv_send_complete").show();
		});		
	})
	
	$(".btn_back").click(function(){
		tinyMCE.execCommand('mceRemoveControl',false,'editor');
		$.colorbox.close();
	})
	

	function buildRTE(host_id){
		tinyMCE.init({
			mode : "textareas",
		    editor_selector : "editor",
			relative_urls : false,
			// General options
			theme : "advanced",
			skin : "cirkuit",
			plugins : "pdw,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,advlist",
			file_browser_callback: 'openKCFinder',
			// Theme options
			theme_advanced_buttons1 : "pdw_toggle,formatselect,fontsizeselect,forecolor,|,bold,italic,strikethrough,|,bullist,numlist,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,spellchecker", 
		    theme_advanced_buttons2 : "code,paste,pastetext,pasteword,removeformat,|,backcolor,|,underline,justifyfull,sup,|,outdent,indent,|,hr,charmap,|,media,|,search,replace,|,fullscreen,|,undo,redo", 
		    theme_advanced_buttons3 : "tablecontrols,|,visualaid,template,pagebreak,preview,emotions", 
			theme_advanced_toolbar_location : "top", 
		    theme_advanced_toolbar_align : "left", 
		    theme_advanced_statusbar_location : "bottom", 
		    theme_advanced_resizing : true, 
		 	height : "300", 
		    width: "650", 
		    convert_urls : false,
			// Example content CSS (should be your site CSS)
			content_css : "css/content.css",
			accessibility_warnings : false,
			pdw_toggle_on : 1,
		    pdw_toggle_toolbars : "2,3",            
			force_br_newlines : true,
			force_p_newlines : false,
		    forced_root_block : '',		    		    
		    setup : function(ed) {
					
			
			ed.onInit.add(function(ed) {
						//alert('Editor is done: ' + ed.id);
						$.post('hosting/load_content',{		
							"host_id":host_id					
						},function(data){
							//tinyMCE.execCommand("mceAddControl",true,'editor');
							tinyMCE.get('editor').setContent(data);
							tinyMCE.execCommand('mceRepaint');							
						})						
			});
					
					/*
					ed.onPreInit.add(function(ed) {
						alert("preinit");
						console.debug('PreInit: ' + ed.id);
					});
					*/

					/*
					ed.onChange.add(function(ed, l) {
						$.get('hosting/load_content',{							
						},function(data){
							//tinyMCE.execCommand("mceAddControl",true,'editor');
							tinyMCE.get('editor').setContent(data);
							tinyMCE.execCommand('mceRepaint');							
						})	
					});
					*/
					
					/*
					ed.onEvent.add(function(ed, e) {
						console.debug('Editor event occured: ' + e.target.nodeName);
					});
					*/
				}
		    
		});
	}
})
//]]>
</script>

<div id="page">
<div id="head"><h1>taSty-dSign.com</h1> <h2>hosting alerts</h2></div>

<div id="search">
  <div id="searchBox">Customer / Website 
    <input type="text" name="textfield" id="textfield" style="width:300px;" />
  <select name="select6" id="select6">
    <option selected="selected">-- All --</option>
    <option>Domain</option>
    <option>Hosting</option>
  </select>
  <input type="submit" name="button9" id="button9" title="ค้นหา" value=" " class="btn_search" /></div>
</div>

<div id="btnBox">
	<input type="button" title="เพิ่มรายการ"  value="เพิ่มรายการ " name="btn_add" id="btn_add"  class=""/>
</div>
<table class="tbList">
<tr>
	<th>Seq.</th>
	<th>Website</th>
	<th>Hosting</th>
	<th>End Date </th>
	<th>Domain </th>
	<th>End Date</th>
	<th>MA</th>
	<th>Customer</th>
	<th>Manage</th>
</tr>
<?php
$i=(@$_GET['page'] > 1)? (((@$_GET['page'])* 20)-20)+1:1;
foreach($host_list  as $key=>$item):	
?>
<tr <?php echo cycle($key)?> >
  <td><?=$i;?></td>
  <td><a href="http://<?=$item['website'];?>" target="_blank"><?=$item['website'];?></a></td>
  <td><?=$item['host_package'];?></td>
  <td>
  	  <? 
  	  $host_expired_date = $item['host_expired_date']>0 ? mysql_to_date($item['host_expired_date'],TRUE,'th') : "";
  	  if($item['host_expired_date']>0)echo mysql_to_date($item['host_expired_date'],TRUE,'th','/');else echo "-";?>	
  </td>
  <td>
  	<? if($item['domain_expired_date']>0)echo "YES";else echo "NO";?>	
  </td>
  <td>
  	  <?
  	  $domain_expired_date = $item['domain_expired_date']>0 ?  mysql_to_date($item['domain_expired_date'],TRUE,'th') : "";
  	  if($item['domain_expired_date']>0)echo mysql_to_date($item['domain_expired_date'],TRUE,'th','/');else echo "-";?>	
  </td>
  <td>
	   <?
	    $ma_start_date = $item['ma_start_date']>0 ? mysql_to_date($item['ma_start_date'],TRUE,'th') : "";
		$ma_end_date =  $item['ma_end_date']>0 ? mysql_to_date($item['ma_end_date'],TRUE,'th','/') : "";
	    if($item['ma_start_date']>0 || $item['ma_end_date']> 0)
	    {
	    	echo $start_date = $item['ma_start_date'] > 0 ? mysql_to_date($item['ma_start_date'],TRUE,'th','/') : "..";
			echo " ถึง ";
			echo $end_date = $item['ma_end_date'] > 0 ? mysql_to_date($item['ma_end_date'],TRUE,'th','/') : "..";
		}
	    else 
	    	echo "-";
		?>
  </td>
  <td><?=$item['customer_name'];?></td>
  <td>
  	<input type="hidden" name="hd_id" id="hd_id" value="<?=$item['id'];?>">
  	<input type="hidden" name="hd_website" id="hd_website" value="<?=$item['website'];?>">
  	<input type="hidden" name="hd_host_package" id="hd_host_package" value="<?=$item['host_package'];?>">
  	<input type="hidden" name="hd_host_expired_date" id="hd_host_expired_date" value="<?=$host_expired_date;?>">
  	<input type="hidden" name="hd_domain_expired_date" id="hd_domain_expired_date" value="<?=$domain_expired_date;?>">
  	<input type="hidden" name="hd_ma_status" id="hd_ma_status" value="<?=$item['ma_status'];?>">
  	<input type="hidden" name="hd_ma_start_date" id="hd_ma_start_date" value="<?=$ma_start_date;?>">
  	<input type="hidden" name="hd_ma_end_date" id="hd_ma_end_date" value="<?=$ma_end_date;?>">
  	<input type="hidden" name="hd_customer_name" id="hd_customer_name" value="<?=$item['customer_name'];?>">
  	<input type="hidden" name="hd_customer_email" id="hd_customer_email" value="<?=$item['customer_email'];?>">
  	<input type="hidden" name="hd_description" id="hd_description" value="<?=$item['description'];?>">
  	<input type="button" name="btn_edit" id="btn_edit" title="แก้ไข" value=" " class="btn_edit" /> 
  	<? if($item['customer_email']!=''){?>
	<input type="button" name="btn_mail" id="btn_mail" title="ส่งเมล์" value=" " class="btn_mail" />
	<? } ?>	
	<a href="hosting/delete/<?php echo $item['id']?><?=$url_parameter;?>"><input type="submit" name="button" id="button" value="" class="btn_delete" onclick="return confirm('<?php echo NOTICE_CONFIRM_DELETE?>')"/></a>
  </td>
</tr>
<? $i++;endforeach; ?>
</table>

<!-- This contains the hidden content for inline calls -->
<div style='display:none'>
	<div id='inline_example1' style='padding:10px; background:#fff;'>
			<div id="dv_host">
			<h2>เพิ่ม/แก้ไขข้อมูลโฮส</h2>
			<form name="host_data" method="post" action="hosting/save<?=$url_parameter;?>">
			<table class="tbAdd">
			<tr>
			<th>Website <span class="TxtRed">*</span></th>
			<td>
				<input type="hidden" name="id" id="id" value="">
				<input name="website" id="website" type="text" style="width:300px;" /> (no fill http://)
			</td>
			</tr>
			<tr>
			<th>Hosting <span class="TxtRed">*</span></th>
			<td>
				<select name="host_package" id="host_package" style="margin-right:20px;">
				  <option value="-" selected="selected">Select Package</option>
				  <option value="No">No</option>
				  <option value="1GB">1GB</option>
				  <option value="2GB">2GB</option>
				  <option value="4GB">4GB</option>
				  <option value="UNLIMITED">UNLIMITED</option>
				</select>
			  	End Date : 
			  	<input name="host_expired_date" id="host_expired_date" type="text"  class="datepicker" />
			</td>
			</tr>
			<tr>
			  <th>Domain <span class="TxtRed">*</span></th>
			  <td> End Date :
			    <input name="domain_expired_date" id="domain_expired_date" type="text"  class="datepicker" />
			  </td>
			</tr>
			<tr>
			  <th>MA <span class="TxtRed">*</span></th>
			  <td>
			  	<select name="ma_status" id="ma_status" style="margin-right:20px;">
			    	<option value="No">No</option>
			    	<option value="Yes">Yes</option>			    	
				</select>
			    <input name="ma_start_date" id="ma_start_date" type="text"  class="datepicker" />
			    <!--<img src="images/calendar.png" width="16" height="16" />--> - 
			    <input name="ma_end_date" id="ma_end_date" type="text" class="datepicker" />
			    </td>
			</tr>
			<tr>
			  <th>Customer  <span class="TxtRed">*</span></th>
			  <td><input name="customer_name" id="customer_name" type="text" style="width:300px;" /></td>
			</tr>
			<tr>
			  <th>Contact Email</th>
			  <td>
			  	<textarea name="customer_email" id="customer_email" style="width:300px;height:100px;"><?=$item['customer_email'];?></textarea> (separate with ",")			  	
			  </td>
			</tr>
			<tr>
			  <th>Description</th>
			  <td>
			  	<textarea name="description" id="description" style="width:300px;height:100px;"><?=$item['description'];?></textarea>			  	
			  </td>
			</tr>
			</table>
			
			<div id="btnSave">
			<input type="submit" title="บันทึก"  value=" Save " class="btn_save"/> 
			<input type="button" title="ย้อนกลับ"  value=" Back " class="btn_back" id="btn_back"/>
			</div>
			</form>
			</div>
			<div id="dv_mail">
				<h2>ส่งข้อความ</h2>
				<table class="tbAdd">
					<tr>
						<th>ชื่อลูกค้า</th>
						<td id="td_customer_name"></td>
					</tr>
					<tr>
						<th>เว็บไซต์</th>
						<td id="td_website"></td>
					</tr>					
					<tr>
						<th>อีเมล์</th>
						<td id="td_customer_email"></td>
					</tr>
					<tr>
						<th>หัวข้อ</th>
						<td>
							<input type="hidden" name="mail_id" id="mail_id">
							<input type="text" name="subject" id="subject" style="width:450px;">
						</td>
					</tr>
					<tr>
						<th>ข้อความ</th>
						<td id="td_message">
									
						<textarea name="editor" id="editor" class="editor" ></textarea>
																							
						</td>
					</tr>					
				</table>
				<div id="btnSave">
					<input type="button" title="ส่งข้อความ" id="btn_send" name="btn_send"  value=" Send " class="btn_send"/> 
					<input type="button" title="ย้อนกลับ"  value=" Back " class="btn_back" id="btn_back"/>
				</div>
		</div>
		<div id="dv_send_complete">
			<h2>ส่งข้อความ</h2>
				<table class="tbAdd">
					<tr>
						<th style="width:450px">ส่งข้อความเรียบร้อย :message has been sent</th>						
					</tr>										
				</table>
				<div id="btnSave">					
					<input type="button" title="ย้อนกลับ"  value=" Back " class="btn_back" id="btn_back"/>
			</div>
		</div>
	</div>
</div>
</div>
